package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.List;
import java.util.Objects;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowThirdPlatformUrl;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowThirdPlatformUrlRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowThirdPlatformUrlInfoDAO;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowThirdPlatformUrlInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowThirdPlatformUrlInfoExample;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-03-05 15:00
 */
@Service
public class FlowThirdPlatformUrlRepositoryImpl implements FlowThirdPlatformUrlRepository {

    @Autowired
    private FlowThirdPlatformUrlInfoDAO flowThirdPlatformUrlInfoDAO;

    @Override
    public FlowThirdPlatformUrl get(String appCode, String type) {
        FlowThirdPlatformUrlInfoExample example = new FlowThirdPlatformUrlInfoExample();
        example.createCriteria().andPlatformCodeEqualTo(appCode).andTypeEqualTo(type);
        List<FlowThirdPlatformUrlInfo> flowThirdPlatformUrlInfos = flowThirdPlatformUrlInfoDAO.selectByExample(example);
        if (CollectionUtils.isNotEmpty(flowThirdPlatformUrlInfos) && Objects.equals(flowThirdPlatformUrlInfos.size(), 1)) {
            return convert(flowThirdPlatformUrlInfos.get(0));
        }
        return null;
    }

    private FlowThirdPlatformUrl convert(FlowThirdPlatformUrlInfo info) {
        if (Objects.isNull(info)) {
            return null;
        }
        FlowThirdPlatformUrl result = new FlowThirdPlatformUrl();
        BeanUtils.copyProperties(info, result);
        return result;
    }
}
